package com.ruoyi.init;

import com.abth.douyin.domain.DouyinCity;
import com.abth.douyin.service.IDouyinCityService;
import com.abth.douyin.util.SpiderResult;
import com.abth.douyin.util.SpiderUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class InitExector_baby implements CommandLineRunner {

    private static final Logger log = LoggerFactory.getLogger(InitExector_baby.class);

    @Autowired
    private SpiderUtil spiderUtil;

    @Autowired
    private IDouyinCityService douyinCityService;

    /*
     * @param args
     * @throws Exception
     */
    @Override
    public void run(String... args) throws Exception {
//        new Thread(()->{
//            try {
//                DouyinCity query = new DouyinCity();
//                query.setStat(1);// 获取未抓取的城市
//                List<DouyinCity> douyinCities = douyinCityService.selectDouyinCityList(query);
//                String[] keywords = {"母婴", "孕"};
//                 for (DouyinCity douyinCity : douyinCities) {
//                     douyinCity.setStat(2);// 抓取中
//                     douyinCityService.updateDouyinCity(douyinCity);
//                     try{
//                         for (String keyword : keywords) {
//                             log.info("===================开始抓取{}-{}:{}============", douyinCity.getPrivinceName(), douyinCity.getCityName(), keyword);
//                             SpiderResult result = spiderUtil.crawShop(keyword, douyinCity.getCityCode(), "052603");
//                             // 抓取异常，终止
//                             if(result.getLevel() == -1){
//                                 log.error(result.getMsg());
//                                 return;
//                             }
//                             log.info("==================={}-{}:{}抓取完成============", douyinCity.getPrivinceName(), douyinCity.getCityName(), keyword);
//                         }
//                         //  抓取成功；
//                         douyinCity.setStat(3);
//                         douyinCityService.updateDouyinCity(douyinCity);
//                     }catch (Exception e){
//                         // 抓取失败
//                         douyinCity.setStat(4);
//                         douyinCityService.updateDouyinCity(douyinCity);
//                     }
//                }
//            } catch (Throwable e) {
//                log.error("异常退出", e);
//                throw new RuntimeException(e);
//            }
//        }).start();

    }
}
